package com.simdo.g768.Dialog;

import static com.simdo.g768.MainActivity.device;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;

import com.simdo.g768.Bean.GnbBean;
import com.simdo.g768.MainActivity;
import com.simdo.g768.R;
import com.simdo.g768.Util.PrefUtil;

public class ParamDialog extends Dialog {

    private final Context mContext;

    public ParamDialog(Context context) {
        super(context, R.style.Theme_B97621);
        this.mContext = context;

        View view = View.inflate(context, R.layout.dialog_param, null);
        this.setContentView(view);
        this.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //this.setCancelable(false);   // 返回键不消失
        //设置dialog大小，这里是一个小赠送，模块好的控件大小设置
        Window window = this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.setNavigationBarColor(Color.parseColor("#2A72FF"));
        //window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        //StatusBarUtil.setLightStatusBar(window, true);
        WindowManager.LayoutParams lp = window.getAttributes();

        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);

        initView(view);
    }
    private void initView(View root) {
        root.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        final Spinner sp_auto_scan_count = root.findViewById(R.id.sp_auto_scan_count);
        sp_auto_scan_count.setSelection(MainActivity.getInstance().scanCount - 1, true);
        sp_auto_scan_count.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                int index = sp_auto_scan_count.getSelectedItemPosition();
                MainActivity.getInstance().scanCount = index + 1;
                PrefUtil.build().putInt("scan_count", index + 1);
                MainActivity.getInstance().showToast(mContext.getString(R.string.updated));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        final Spinner sp_near = root.findViewById(R.id.sp_near);
        sp_near.setSelection(Math.abs(MainActivity.getInstance().nearValue) - 3, true);
        sp_near.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                int value = Integer.parseInt(sp_near.getSelectedItem().toString());
                MainActivity.getInstance().nearValue = value;
                PrefUtil.build().putInt("near_value", value);
                MainActivity.getInstance().showToast(mContext.getString(R.string.updated));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        final Switch switch_mob_reject_code = root.findViewById(R.id.switch_mob_reject_code);
        boolean mob_reject_code = PrefUtil.build().getBoolean("mob_reject_code", false);
        switch_mob_reject_code.setChecked(mob_reject_code);
        switch_mob_reject_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (device != null && device.getWorkState() != GnbBean.State.IDLE){
                    switch_mob_reject_code.setChecked(!switch_mob_reject_code.isChecked());
                    MainActivity.getInstance().showToast(mContext.getString(R.string.in_work_not_do));
                    return;
                }
                PrefUtil.build().putBoolean("mob_reject_code", switch_mob_reject_code.isChecked());
                MainActivity.getInstance().showToast(mContext.getString(R.string.updated));
            }
        });

        final Switch switch_force_cfg = root.findViewById(R.id.switch_force_cfg);
        boolean force_cfg = PrefUtil.build().getBoolean("force_cfg", false);
        switch_force_cfg.setChecked(force_cfg);
        switch_force_cfg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (device != null && device.getWorkState() != GnbBean.State.IDLE){
                    switch_force_cfg.setChecked(!switch_force_cfg.isChecked());
                    MainActivity.getInstance().showToast(mContext.getString(R.string.in_work_not_do));
                    return;
                }
                PrefUtil.build().putBoolean("force_cfg", switch_force_cfg.isChecked());
                MainActivity.getInstance().showToast(mContext.getString(R.string.updated));
            }
        });

        final EditText ed_tac_interval = root.findViewById(R.id.ed_tac_interval);
        ed_tac_interval.setText(PrefUtil.build().getString("tac_interval", "180"));
        final TextView tv_save_tac = root.findViewById(R.id.tv_save_tac);
        tv_save_tac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tac_interval = ed_tac_interval.getText().toString();
                if (tac_interval.isEmpty()) {
                    MainActivity.getInstance().showToast(mContext.getString(R.string.not_empty));
                    return;
                }
                PrefUtil.build().putString("tac_interval", tac_interval);
                MainActivity.getInstance().showToast(mContext.getString(R.string.updated));
            }
        });
    }
}
